﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace UmlDesigner.Model
{
    [Serializable]
    class UmlMethod
    {
        internal string accessLevel = "internal";
        internal string returnType = "void";
        internal string name = "NewMethod";
        internal string virtualModifier = "-- None --";
        internal string implementation = "{\n    throw new NotImplementedException();\n}\n";
        internal List<Argument> arguments = new List<Argument>();

        /// <summary>
        /// Copies the method and everything associated with it.
        /// </summary>
        /// <param name="workingMethod"></param>
        internal void DeepCopy(UmlMethod target)
        {
            target.accessLevel = accessLevel;
            target.returnType = returnType;
            target.name = name;
            target.virtualModifier = virtualModifier;
            target.implementation = implementation;

            // Clone the list of arguments.
            target.arguments = new List<Argument>();
            foreach (Argument a in arguments)
            {
                Argument arg = new Argument();
                arg.name = a.name;
                arg.type = a.type;
                target.arguments.Add(arg);
            }
        }

        internal string ArgsToString()
        {
            StringBuilder argsList = new StringBuilder();
            argsList.Append("(");
            foreach (Argument a in arguments)
            {
                if (argsList.Length != 1)
                {
                    argsList.Append(", ");
                }
                argsList.Append(a.type);
                argsList.Append(" ");
                argsList.Append(a.name);
            }
            argsList.Append(")");
            return argsList.ToString();
        }

        internal void GenerateCode(CodeGenerationSettings settings)
        {
            settings.WriteIndentedLine(accessLevel + " " + GetVirtualModifier() + returnType + " " + name + ArgsToString());

            foreach (string line in implementation.Split('\n'))
            {
                settings.WriteIndentedLine(line);
            }
        }

        private string GetVirtualModifier()
        {
            if (virtualModifier == "-- None --")
            {
                return "";
            }
            else
            {
                return virtualModifier + " ";
            }
        }

        internal string GetAsDisplayableString()
        {
            return returnType + " " + name + ArgsToString();
        }
    }
}
